/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.football.lottery.ui;

import com.football.lottery.obj.FootballLottery;
import com.football.lottery.obj.RecommendedItem;
import com.football.lottery.obj.GameItem;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;

/**
 *
 * @author I am god
 */
public class ExcelParser
{
  private String filePath;

  public ExcelParser(String filePath)
  {
    this.filePath = filePath;
  }
  
  public List getAllFootballLottery() throws FileNotFoundException, IOException, ParseException
  {
    List fblList = new ArrayList();
    FileInputStream is = new FileInputStream(filePath);
    HSSFWorkbook wb = new HSSFWorkbook(is);
    HSSFSheet sheet = wb.getSheetAt(0);
    
    int currRow = 0;
    HSSFRow row = sheet.getRow(currRow);
    
    while(row != null)
    {
      fblList.add(this.getFootballLottery(sheet, currRow));
      currRow += 17;
      row = sheet.getRow(currRow);
    }

    return fblList;
  }

  public FootballLottery getFootballLottery(HSSFSheet sheet, int currRow) throws ParseException
  {
    FootballLottery fbl = new FootballLottery();
    List<GameItem> games = new ArrayList<GameItem>();
    StringBuffer result = new StringBuffer();

    if (sheet != null)
    {
        HSSFRow row = sheet.getRow(currRow);
        int periodNo = (int) row.getCell(0).getNumericCellValue();

        String stopTime = row.getCell(1).getStringCellValue().trim();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        fbl.setPeriodNo(periodNo);
        fbl.setStopTime(stopTime);
        currRow += 2;

        row = sheet.getRow(currRow);

        while (row != null)
        {
          GameItem game = new GameItem();

          game.setPeriodNo(periodNo);
          int orderNo = (int) row.getCell(0).getNumericCellValue();
          game.setOrderNo(orderNo);
          game.setGameType(row.getCell(1).getStringCellValue());

          // Get game time.
          Date gameTime = null;
          try
          {
            gameTime = row.getCell(2).getDateCellValue();
          }
          catch (IllegalStateException ex)
          {
            String timeStr = row.getCell(2).getStringCellValue();
            df = new SimpleDateFormat("MM-dd hh:mm:ss");
            gameTime = df.parse(timeStr);
            gameTime.setYear(new Date().getYear());
          }
          game.setGameTime(gameTime);

          game.setHostName(row.getCell(3).getStringCellValue());
          
          HSSFCell scoreCell = row.getCell(4);
          game.setScoreline(scoreCell.getCellType() == Cell.CELL_TYPE_BLANK  ?  "" : scoreCell.getStringCellValue());
          
          game.setGuestName(row.getCell(5).getStringCellValue());
          
          HSSFCell resultCell = row.getCell(6);
          result.append(resultCell.getCellType() == Cell.CELL_TYPE_BLANK ? "X" : (int) resultCell.getNumericCellValue() + "");

          game.setCompensationRate(row.getCell(8).getStringCellValue().trim());
          System.out.println(game.toString());
          games.add(game);
          currRow++;
          if (orderNo == 14)
          {
            break;
          }
          row = sheet.getRow(currRow);
        }
        fbl.setResultStr(result.toString());
        fbl.setGames(games);
      }
    return fbl;
  }

  public List<RecommendedItem> getRecommendedData(String filePath) throws FileNotFoundException, IOException
  {
    // todo
    
    return null;
  }
}
